﻿using GKQC.Common;
using GKQC.DbHelper.DbModels;
using GKQC.DbHelper.Services;
using GKQC.Language;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.Util;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace GKQC.Ort.Reports
{
    /// <summary>
    /// 信赖性试验：信赖性试验总览
    /// </summary>
    public class R999 
    {
        public IWorkbook GenReport(List<OrtHeadModel> heads, ReportSetModel reportSet, string path, bool isCreateFile)
        {
            string errorMsg = string.Empty;
            string fileName = Settings.GetReportFileName(path, "实验室\\信赖性试验总览.xls", out errorMsg);
            if (!string.IsNullOrWhiteSpace(errorMsg))
            {
                QMessageBox.Show(errorMsg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return null;
            }

            if (heads == null || heads.Count == 0) return null;

            string sysTempFileName = Settings.GetTempPath() + "\\"
                + OrtReportFactory.GetExcelTempFileName(heads[0], reportSet)//+ Guid.NewGuid().ToString()
                + "-"
                + fileName.Substring(fileName.LastIndexOf('\\') + 1)
                ;
            File.Copy(fileName, sysTempFileName, true);

            IWorkbook workbook = null;
            using (FileStream fs = new FileStream(sysTempFileName, FileMode.Open, FileAccess.Read))  //路径，打开权限，读取权限
            {
                workbook = WorkbookFactory.Create(new FileInputStream(fs));
                fs.Close();
            }
            ISheet sheet = workbook.GetSheetAt(0);

            try { GenReportSheet(workbook, sheet, heads); } catch (Exception ex) { Logger.LogError(ex); }

            if (isCreateFile)
            {
                using (FileStream fs = new FileStream(sysTempFileName, FileMode.Create, FileAccess.Write))
                {
                    MemoryStream stream = new MemoryStream();
                    workbook.Write(stream);
                    var buf = stream.ToArray();
                    fs.Write(buf, 0, buf.Length);
                    fs.Flush();
                }

                Process.Start(sysTempFileName);
            }

            return workbook;
        }

        public void GenReportSheet(IWorkbook workbook, ISheet sheet, List<OrtHeadModel> heads)
        {
            if (heads == null || heads.Count == 0) return;

            OrtService service = new OrtService(new DbHelper.Dao.AllDbContext());

            #region 表头
            int headColumnsIndex = 0;
            int firstDataRowIndex = 2;
            for (int i = 0; i < heads.Count; i++)
            {
                try
                {
                    if(i>0) sheet.CopyRow(firstDataRowIndex, firstDataRowIndex + i);
                    OrtHeadModel head = heads[i];
                    sheet.GetRow(firstDataRowIndex + i).GetCell(headColumnsIndex + 0).SetCellValue(i + 1);//序號
                    sheet.GetRow(firstDataRowIndex + i).GetCell(headColumnsIndex + 1).SetCellValue(head.Pn);//成品編號
                    sheet.GetRow(firstDataRowIndex + i).GetCell(headColumnsIndex + 2).SetCellValue(head.LotNo);//成品編號
                    sheet.GetRow(firstDataRowIndex + i).GetCell(headColumnsIndex + 3).SetCellValue(head.Series);//系列
                    sheet.GetRow(firstDataRowIndex + i).GetCell(headColumnsIndex + 4).SetCellValue(head.Cap);//容量
                    sheet.GetRow(firstDataRowIndex + i).GetCell(headColumnsIndex + 5).SetCellValue(head.Voltage);//電壓
                    sheet.GetRow(firstDataRowIndex + i).GetCell(headColumnsIndex + 6).SetCellValue(head.Size);//尺寸
                    sheet.GetRow(firstDataRowIndex + i).GetCell(headColumnsIndex + 7).SetCellValue(head.FoilP);
                    sheet.GetRow(firstDataRowIndex + i).GetCell(headColumnsIndex + 8).SetCellValue(head.FoilpSupplier);
                    sheet.GetRow(firstDataRowIndex + i).GetCell(headColumnsIndex + 9).SetCellValue(head.FoilN);
                    sheet.GetRow(firstDataRowIndex + i).GetCell(headColumnsIndex + 10).SetCellValue(head.Eps);
                    sheet.GetRow(firstDataRowIndex + i).GetCell(headColumnsIndex + 11).SetCellValue(head.Paste);
                    sheet.GetRow(firstDataRowIndex + i).GetCell(headColumnsIndex + 12).SetCellValue(head.Rubber);
                    sheet.GetRow(firstDataRowIndex + i).GetCell(headColumnsIndex + 13).SetCellValue(head.DateCode);
                    sheet.GetRow(firstDataRowIndex + i).GetCell(headColumnsIndex + 14).SetCellValue(head.TestTotalTime);
                    sheet.GetRow(firstDataRowIndex + i).GetCell(headColumnsIndex + 15).SetCellValue(head.Remark);//Life测试结果
                    sheet.GetRow(firstDataRowIndex + i).GetCell(headColumnsIndex + 16).SetCellValue(head.BadReason);//Life测试结果
                    sheet.GetRow(firstDataRowIndex + i).GetCell(headColumnsIndex + 23).SetCellValue(head.BadDesc);//Life测试结果
                    if (head.Dtls.Count >0)
                        sheet.GetRow(firstDataRowIndex + i).GetCell(headColumnsIndex + 16).SetCellValue(
                            head.Dtls[head.Dtls.Count-1].TestTime); //當前測試時間
                    if(string.IsNullOrWhiteSpace(head.Customer))
                        sheet.GetRow(firstDataRowIndex + i).GetCell(headColumnsIndex + 17).SetCellValue(head.Customer);
                    else
                        sheet.GetRow(firstDataRowIndex + i).GetCell(headColumnsIndex + 17).SetCellValue(head.Customer);
                    sheet.GetRow(firstDataRowIndex + i).GetCell(headColumnsIndex + 18).SetCellValue(head.SampleDate);
                    sheet.GetRow(firstDataRowIndex + i).GetCell(headColumnsIndex + 19).SetCellValue(head.TestDate);//投入日期
                    if (head.Dtls.Count > 0) 
                        sheet.GetRow(firstDataRowIndex + i).GetCell(headColumnsIndex + 20).SetCellValue(
                            head.Dtls[head.Dtls.Count - 1].TestDate);//當前測試日期
                    if(head.ExpireDate.HasValue)
                        sheet.GetRow(firstDataRowIndex + i).GetCell(headColumnsIndex + 21).SetCellValue(head.ExpireDate.Value);//下次測試日期
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex);
                }
            }
            #endregion 表头


            sheet.ForceFormulaRecalculation = true;
        }
    }
}
